﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
''' <summary>
''' Everybody know this.
''' Everybody uses it.
''' But nobody wrote it yet, for Commander Wars.
''' </summary>
''' <remarks></remarks>
Public Class Chat
    Inherits Objekt2D
    Enum ChatZiele
        Alle
        Allierte
        Feinde
    End Enum
    Enum Modus
        Lobby
        CO_Screen
        Game
    End Enum
    Const Historylength As Integer = 2048
    Const Rightborder As Integer = 200
    Public Const TopBorder As Integer = 40
    Const HiddenVisibleFrames As Integer = 30
    Const HiddenUnVisibleFrames As Integer = 10
    ''' <summary>
    ''' The Chat History!
    ''' </summary>
    ''' <remarks></remarks>
    Private History As New List(Of Message)
    ''' <summary>
    ''' Represents a recieveid Message
    ''' </summary>
    ''' <remarks></remarks>
    Structure Message
        Dim text As String
        Dim Author As String
        Dim Spieler As Integer
        ''' <summary>
        ''' Well, let's create a new Message.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New(ByVal text As String, ByVal Author As String, Optional ByVal Spieler As Integer = -1)
            Me.text = text.Replace(Environment.NewLine, "")
            Me.Author = Author
            Me.Spieler = Spieler
        End Sub
        ''' <summary>
        ''' What's the Message
        ''' </summary>
        ''' <returns>the Message</returns>
        ''' <remarks></remarks>
        Public Function getMessage() As String
            Return Author + ": " + text
        End Function
        Public Overrides Function ToString() As String
            Return Spieler.ToString + "|" + Author + "|" + text
        End Function
    End Structure
    Public Sub ClearHistory()
        History.Clear()
    End Sub
    Public Sub getMessage(ByVal m As Message)
        History.Add(m)
        Scrollbar.SetYPos(Height)
        Visible = True
        HiddenFrame = 0
    End Sub
    Private Sub SendMessage(ByVal Text As String, ByVal Teams() As Char)
        'Create Message
        Dim Mess As String = "Chat|"
        For I = 0 To Teams.Length - 1
            If I = Teams.Length - 1 Then
                Mess += Teams(I)
            Else
                Mess += Teams(I) + "_"
            End If
        Next
        Dim Player As Integer = -1
        If Objektverwalter.Spielfeld IsNot Nothing And TypeOf Objektverwalter.Menue Is ObjektSpielMenue Then
            For i = 1 To Objektverwalter.Spielfeld.Spieler.Length - 1
                If Objektverwalter.Spielfeld.Spieler(i).KIStufe = ObjektSpieler.KIStufen.Mensch Then
                    Player = i
                    Exit For
                End If
            Next
        End If
        Mess += "|" + DataSettings.Autor + "|" + Text + "|" + Player.ToString
        'Send Message
        Dim Sended As Boolean = False
        If Hauptmenue.Connection IsNot Nothing Then
            If Hauptmenue.Connection.Connected Then

                Hauptmenue.Connection.SendData(New NetworkPaket(Mess))
                Sended = True
            End If
        End If
        If Not Sended Then
            If Hauptmenue.Lobby IsNot Nothing Then
                If Hauptmenue.Lobby.Connection IsNot Nothing Then
                    If Hauptmenue.Lobby.Connection.Connected Then
                        Hauptmenue.Lobby.Connection.SendData(Mess)
                    End If
                End If
            End If
        End If
        'Add Message to History

        History.Add(New Message(Text, DataSettings.Autor, Player))
        Scrollbar.SetYPos(Height)
    End Sub
    Private Sub CheckHistoryLength()
        While History.Count > Historylength
            History.RemoveAt(0)
        End While
    End Sub

    Dim Scrollbar As ObjektYScrollbar
    Dim Textbox As ObjektTextbox

    Dim DropDownMenue As ObjektDropDownMenue
    Dim Checkbox As ObjektCheckBox
    Public Chatmodus As Modus
    Public Visible As Boolean = True

    Dim HiddenFrame As Integer = 0
    Dim Contained As Boolean
    Dim Font As Integer
    Public Sub New(ByVal Top As Boolean, ByVal Chatmodus As Modus, Optional ByVal Height As Integer = 155)
        MyBase.New("", New Rectangle, New Point, New PointF)
        Me.Height = Height
        Me.Top = Top
        Me.ChatFont = Schriftengine.CreateSchriftart(Schriftengine.Schriftarten.Courier_New, 10, FontStyle.Italic)
        Me.Chatmodus = Chatmodus
        CreateSubObjects()
        Font = Schriftengine.CreateSchriftart(Schriftengine.Schriftarten.Arial, 12, FontStyle.Regular)
    End Sub
    Public Sub CreateSubObjects()
        If Scrollbar IsNot Nothing Then
            Objekt2D.RemoveObjekt(Scrollbar)
        End If
        If Top Then
            Scrollbar = New ObjektYScrollbar(0, Height - 22, New Point(DataSettings.BildschirmBreite - Rightborder + 5, TopBorder), New PointF(0, 0))
        Else
            Scrollbar = New ObjektYScrollbar(0, Height - 22, New Point(DataSettings.BildschirmBreite - Rightborder + 5, DataSettings.BildschirmHoehe - Height), New PointF(0, 0))
        End If
        Scrollbar.SetYPos(Height)
        If Top Then
            Textbox = New ObjektTextbox(5, 5, DataSettings.BildschirmBreite - Rightborder, New PointF)
        Else
            Textbox = New ObjektTextbox(5, DataSettings.BildschirmHoehe - Height - TopBorder + 5, DataSettings.BildschirmBreite - Rightborder, New PointF)
        End If
        'Remove it from Main-Drawing!
        Objekt2D.RemoveObjekt(Textbox)
        If Chatmodus = Modus.Game Then
            'Dropdownmenue for Teams
            Dim Items As New List(Of String)
            Items.AddRange([Enum].GetNames(ChatZiele.Alle.GetType))
            For I = 1 To Objektverwalter.Spielfeld.Spieler.Length - 1
                If Not Items.Contains(Objektverwalter.Spielfeld.Spieler(I).Team) Then
                    Items.Add(Objektverwalter.Spielfeld.Spieler(I).Team)
                End If
            Next
            If Top Then
                DropDownMenue = New ObjektDropDownMenue(DataSettings.BildschirmBreite - Rightborder + 30, 5, Items.ToArray, New PointF)
            Else
                DropDownMenue = New ObjektDropDownMenue(DataSettings.BildschirmBreite - Rightborder + 30, DataSettings.BildschirmHoehe - Height - TopBorder + 5, Items.ToArray, New PointF)
            End If
            Objekt2D.RemoveObjekt(DropDownMenue)
            If Top Then
                If Checkbox Is Nothing Then
                    Checkbox = New ObjektCheckBox(New Point(DataSettings.BildschirmBreite - 30, 60), "CheckBox", New PointF, False, )
                Else
                    Checkbox = New ObjektCheckBox(New Point(DataSettings.BildschirmBreite - 30, 60), "CheckBox", New PointF, Checkbox.Checked, )
                End If
            Else
                If Checkbox Is Nothing Then
                    Checkbox = New ObjektCheckBox(New Point(DataSettings.BildschirmBreite - 30, DataSettings.BildschirmHoehe - Height - TopBorder + 60), "CheckBox", New PointF, False, )
                Else
                    Checkbox = New ObjektCheckBox(New Point(DataSettings.BildschirmBreite - 30, DataSettings.BildschirmHoehe - Height - TopBorder + 60), "CheckBox", New PointF, Checkbox.Checked, )
                End If
            End If
            Objekt2D.RemoveObjekt(Checkbox)
        End If
    End Sub
    Public Overrides Function Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        If Visible Then
            Select Case e.KeyCode
                Case Keys.Enter
                    SendMessage(Textbox.GetText, GetSendTeams)
                    Textbox.SetText("")
                Case Else
                    Scrollbar.Keydown(sender, e)
                    Return Textbox.Keydown(sender, e)
            End Select
        End If
    End Function
    Private Function GetSendTeams() As Char()
        If DropDownMenue Is Nothing Then
            Return New Char() {}
        Else
            Dim Item As String = DropDownMenue.GetItem
            If [Enum].GetNames(ChatZiele.Alle.GetType).Contains(Item) Then
                Dim Ziele As ChatZiele = [Enum].Parse(ChatZiele.Alle.GetType, Item)
                Select Case Ziele
                    Case ChatZiele.Allierte
                        Dim Team As Char = ""
                        For I = 1 To Objektverwalter.Spielfeld.Spieler.Length - 1
                            If Objektverwalter.Spielfeld.Spieler(I).KIStufe = ObjektSpieler.KIStufen.Mensch Then
                                Team = Objektverwalter.Spielfeld.Spieler(I).Team
                                Exit For
                            End If
                        Next
                        If Team <> "" Then
                            Return New Char() {Team}
                        Else
                            Return New Char() {}
                        End If
                    Case ChatZiele.Feinde
                        Dim Team As Char = ""
                        For I = 1 To Objektverwalter.Spielfeld.Spieler.Length - 1
                            If Objektverwalter.Spielfeld.Spieler(I).KIStufe = ObjektSpieler.KIStufen.Mensch Then
                                Team = Objektverwalter.Spielfeld.Spieler(I).Team
                                Exit For
                            End If
                        Next
                        If Team <> "" Then
                            Dim Teams As New List(Of Char)
                            For I = 1 To Objektverwalter.Spielfeld.Spieler.Length - 1
                                If Objektverwalter.Spielfeld.Spieler(I).Team <> Team Then
                                    Teams.Add(Objektverwalter.Spielfeld.Spieler(I).Team)
                                End If
                            Next
                            Return Teams.ToArray
                        Else
                            Return New Char() {}
                        End If
                    Case Else
                        Return New Char() {}
                End Select
            Else
                Return New Char() {Item}
            End If
        End If
    End Function
    Public Overrides Function KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
        If Visible Then
            Scrollbar.KeyUp(sender, e)
            Textbox.KeyUp(sender, e)
        End If
    End Function

    Public Overloads Overrides Function MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If Visible Then
            Scrollbar.MouseDown(sender, e)
            Textbox.MouseDown(sender, e)
            If DropDownMenue IsNot Nothing Then
                If DropDownMenue.Focused Then
                    DropDownMenue.MouseDown(sender, e)
                    Return True
                Else
                    DropDownMenue.MouseDown(sender, e)
                End If
            End If
            If Checkbox IsNot Nothing Then
                Checkbox.MouseDown(sender, e)
            End If
            If Top Then
                If e.Y < Height + TopBorder Then
                    Return True
                End If
            Else
                If e.Y > DataSettings.BildschirmHoehe - Height - TopBorder Then
                    Return True
                End If
            End If
        End If
        Return False
    End Function

    Public Overloads Overrides Function MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If Checkbox IsNot Nothing Then
            If Not Checkbox.Checked Then
                Contains(e)
            End If
        ElseIf Chatmodus = Modus.CO_Screen Then
            Contains(e)
        Else
            Visible = True
        End If
        If Visible Then
            Scrollbar.MouseEnter(sender, e)
            Textbox.MouseEnter(sender, e)
            If DropDownMenue IsNot Nothing Then
                DropDownMenue.MouseEnter(sender, e)
            End If
            If Checkbox IsNot Nothing Then
                Checkbox.MouseEnter(sender, e)
            End If
        End If
    End Function

    Public Overloads Overrides Function MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As String
        If Visible Then
            Scrollbar.MouseUp(sender, e)
            Textbox.MouseUp(sender, e)
            If DropDownMenue IsNot Nothing Then
                DropDownMenue.MouseUp(sender, e)
            End If
            If Checkbox IsNot Nothing Then
                Checkbox.MouseUp(sender, e)
            End If
            If Top Then
                If e.Y < Height + TopBorder Then
                    Return "Clicked"
                End If
            Else
                If e.Y > DataSettings.BildschirmHoehe - Height - TopBorder Then
                    Return "Clicked"
                End If
            End If
        End If
        Return ""
    End Function

    Public Overloads Overrides Function MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
        If Visible Then
            Scrollbar.MouseWheel(sender, e)
            Textbox.MouseWheel(sender, e)
            If DropDownMenue IsNot Nothing Then
                DropDownMenue.MouseWheel(sender, e)
            End If
            If Checkbox IsNot Nothing Then
                Checkbox.MouseWheel(sender, e)
            End If
        End If
    End Function
    Private Sub Contains(ByVal e As System.Windows.Forms.MouseEventArgs)
        If Visible Then
            If Top Then
                If e.Y < TopBorder + Height Then
                    DoHiddenFrame(True)
                Else
                    DoHiddenFrame(False)
                End If
            Else
                If e.Y > DataSettings.BildschirmHoehe - TopBorder - Height Then
                    DoHiddenFrame(True)
                Else
                    DoHiddenFrame(False)
                End If
            End If
        Else
            If Top Then
                If e.Y < TopBorder Then
                    DoHiddenFrame(True)
                Else
                    DoHiddenFrame(False)
                End If
            Else
                If e.Y > DataSettings.BildschirmHoehe - TopBorder Then
                    DoHiddenFrame(True)
                Else
                    DoHiddenFrame(False)
                End If
            End If
        End If
    End Sub
    Private Sub DoHiddenFrame(ByVal Contains As Boolean)
        If Contained <> Contains Then
            Contained = Contains
            HiddenFrame = 0
        End If
        HiddenFrame += 1
        If Visible Or Chatmodus = Modus.CO_Screen Then
            If HiddenVisibleFrames <= HiddenFrame Then
                HiddenFrame = HiddenVisibleFrames
                If Contains Then
                    Visible = True
                Else
                    Textbox.RemoveFocus()
                    If DropDownMenue IsNot Nothing Then
                        DropDownMenue.RemoveFocus()
                    End If
                    HiddenFrame = 0
                    Visible = False
                End If
            End If
        Else
            If HiddenUnVisibleFrames <= HiddenFrame Then
                HiddenFrame = HiddenUnVisibleFrames
                If Contains Then
                    Visible = True
                Else
                    Textbox.RemoveFocus()
                    If DropDownMenue IsNot Nothing Then
                        DropDownMenue.RemoveFocus()
                    End If
                    HiddenFrame = 0
                    Visible = False
                End If
            End If
        End If

    End Sub
#Region "Drawing"
    Public Top As Boolean
    Public Height As Integer = 155
    Dim ChatFont As Integer = -1
    Public Overrides Sub Malen()
        If Visible Then
            Dim YStart As Integer = 0
            If Top Then
                YStart = TopBorder
            Else
                YStart = DataSettings.BildschirmHoehe - Height
            End If
            'Draw Background
            Grafikengine.fillRect(New Rectangle(New Point(0, YStart - TopBorder), New Size(DataSettings.BildschirmBreite, Height + TopBorder)), Color.Gray)
            'Draw Objects
            Scrollbar.Malen()
            Textbox.Malen()
            If DropDownMenue IsNot Nothing Then
                DropDownMenue.Malen()
            End If
            If Checkbox IsNot Nothing Then
                If Top Then
                    Schriftengine.DrawTextZeile(Sprachconverter.Translate("Fest"), New Point(DataSettings.BildschirmBreite - Rightborder + 30, 60), Font, Color.Black)
                Else
                    Schriftengine.DrawTextZeile(Sprachconverter.Translate("Fest"), New Point(DataSettings.BildschirmBreite - Rightborder + 30, DataSettings.BildschirmHoehe - Height - TopBorder + 60), Font, Color.Black)
                End If
                Checkbox.Malen()
            End If
            'Draw Text
            Dim KonsolenGroesse As New Size
            KonsolenGroesse.Width = DataSettings.BildschirmBreite - Rightborder
            KonsolenGroesse.Height = Height

            Grafikengine.Sprite.Transform = Matrix.Identity
            Dim BuchstabenGroesse As Size = Schriftengine.Fonts(ChatFont).GetFont.MeasureString(Grafikengine.Sprite, "H", Microsoft.DirectX.Direct3D.DrawTextFormat.None, Color.Black).Size
            Dim BuchstabenZahlMax As Integer = Math.Round(KonsolenGroesse.Width / BuchstabenGroesse.Width, System.MidpointRounding.ToEven)
            Dim MaxZeilenZahl As Integer = Math.Round(KonsolenGroesse.Height / BuchstabenGroesse.Height, System.MidpointRounding.ToEven) - 1

            Dim Textstart As Integer = History.Count * Scrollbar.GetValuePos - MaxZeilenZahl 'Add Numeric Up down here
            If Textstart < 0 Then Textstart = 0
            Dim Textzeilen As Integer = 0
            Dim MomZeilen As Integer = 0
            While MomZeilen < Historylength And Textzeilen + Textstart < History.Count And MomZeilen < MaxZeilenZahl

                Dim TextLaenge As Integer = History(Textstart).Author.Length + 2 + History(Textstart).text.Length
                If Math.Round((TextLaenge / BuchstabenZahlMax), MidpointRounding.AwayFromZero) + MomZeilen < MaxZeilenZahl Then

                    Textzeilen += 1
                    MomZeilen += 1
                    TextLaenge -= (BuchstabenZahlMax)

                    'Textabschneiden.
                    While TextLaenge > 0
                        MomZeilen += 1
                        TextLaenge -= BuchstabenZahlMax
                    End While
                Else
                    Exit While
                End If
            End While


            Dim First As Boolean = True
            For I = Textstart To Textstart + Textzeilen - 1
                Dim Text As String = History(I).Author + ": " + History(I).text
                Dim Zeilen As New List(Of String)
                Dim I2 As Integer = 1
                If Text.Length > BuchstabenZahlMax Then
                    Zeilen.Add(Text.Substring(0, BuchstabenZahlMax))
                    While Text.Length > I2 * BuchstabenZahlMax
                        Dim Buchstabenzahl As Integer = BuchstabenZahlMax
                        If Text.Length - (I2 * BuchstabenZahlMax) < Buchstabenzahl Then
                            Buchstabenzahl = Text.Length - (I2 * BuchstabenZahlMax)
                        End If
                        Zeilen.Add(Text.Substring(I2 * BuchstabenZahlMax, Buchstabenzahl))
                        I2 += 1
                    End While
                Else
                    Zeilen.Add(Text)
                End If

                If First Then
                    Schriftengine.DrawTextZeile(Zeilen, New Point(0, YStart), ChatFont, Color.Red)
                Else
                    Schriftengine.DrawTextZeile(Zeilen, New Point(0, YStart), ChatFont, Color.Blue)
                End If
                First = Not First
                YStart += Zeilen.Count * BuchstabenGroesse.Height
            Next
            If Hauptmenue.Chatroom.Top Then
                If ObjektMaus.YKoordinate < Hauptmenue.Chatroom.Height + TopBorder Then
                    ObjektMaus.Visible = True
                Else
                    If TypeOf Objektverwalter.Menue Is ObjektSpielMenue Then
                        If Objektverwalter.Spielfeld.AktuellerSpieler.KIStufe <> ObjektSpieler.KIStufen.Mensch Then
                            ObjektMaus.Visible = False
                        End If
                    End If
                End If
            Else
                If ObjektMaus.YKoordinate > DataSettings.BildschirmHoehe - Hauptmenue.Chatroom.Height - TopBorder Then
                    ObjektMaus.Visible = True
                Else
                    If TypeOf Objektverwalter.Menue Is ObjektSpielMenue Then
                        If Objektverwalter.Spielfeld.AktuellerSpieler.KIStufe <> ObjektSpieler.KIStufen.Mensch Then
                            ObjektMaus.Visible = False
                        End If
                    End If
                End If
            End If
        Else
            If Hauptmenue.Chatroom.Top Then
                If ObjektMaus.YKoordinate > Hauptmenue.Chatroom.Height + TopBorder Then
                    If TypeOf Objektverwalter.Menue Is ObjektSpielMenue Then
                        If Objektverwalter.Spielfeld.AktuellerSpieler.KIStufe <> ObjektSpieler.KIStufen.Mensch Then
                            ObjektMaus.Visible = False
                        End If
                    End If
                End If
            Else
                If ObjektMaus.YKoordinate < DataSettings.BildschirmHoehe - Hauptmenue.Chatroom.Height - TopBorder Then
                    If TypeOf Objektverwalter.Menue Is ObjektSpielMenue Then
                        If Objektverwalter.Spielfeld.AktuellerSpieler.KIStufe <> ObjektSpieler.KIStufen.Mensch Then
                            ObjektMaus.Visible = False
                        End If
                    End If
                End If
            End If
        End If
    End Sub
#End Region

End Class
